package com.dragon.logical.service.finance.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.model.ResultCode;
import com.dragon.common.model.exception.CustomException;
import com.dragon.logical.service.finance.IExpensesService;
import com.dragon.repository.entity.Expenses;
import com.dragon.repository.entity.Loan;
import com.dragon.repository.entity.Reimbursement;
import com.dragon.repository.mapper.ExpensesMapper;
import com.dragon.repository.mapper.LoanMapper;
import com.dragon.repository.mapper.ReimbursementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sy
 * @since 2023-11-24
 */
@Service
public class ExpensesServiceImpl extends ServiceImpl<ExpensesMapper, Expenses> implements IExpensesService {

    @Autowired
    ExpensesMapper expensesMapper;
    @Autowired
    ReimbursementMapper reimbursementMapper;
    @Autowired
    LoanMapper loanMapper;

    @Override
    public Page<Expenses> expensesList(Integer page, Integer limit, Integer type, Integer isDisable) {
        Page<Expenses> page1 = new Page<>(page, limit);
        QueryWrapper<Expenses> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(type != null,Expenses::getType, type)
                .eq(isDisable != null, Expenses::getDeleted,isDisable);
        Page<Expenses> expensesPage = expensesMapper.selectPage(page1, wrapper);
        return expensesPage;
    }


    @Override
    public void addToExpenses(Expenses expenses) {
        expenses.setCreateTime(DateUtil.date());
        expensesMapper.insert(expenses);
    }

    @Override
    public void updateExpenses(Expenses expenses) {
        Expenses expenses1 = expensesMapper.selectById(expenses.getId());
        if (expenses1 == null){
            throw new CustomException(ResultCode.DATA_NOT_FOUND);
        }
        expenses.setUpdateTime(DateUtil.date());
        expensesMapper.updateById(expenses);
    }

    @Override
    public void disableEnable(Integer id) {
        Expenses expenses = expensesMapper.selectById(id);
        Expenses expenses1 = new Expenses();
        QueryWrapper<Expenses> wrapper = new QueryWrapper<>();
        if (expenses.getDeleted() ==0 ){
            List<Reimbursement> reimbursements = reimbursementMapper.selectList(new QueryWrapper<Reimbursement>().lambda()
                    .eq(Reimbursement::getReimburseType, id));
            List<Loan> loans = loanMapper.selectList(new QueryWrapper<Loan>().lambda().eq(Loan::getLoanType, id));
            if (reimbursements.size() !=0 || loans.size() != 0 ){
                throw new CustomException(ResultCode.EXPENSES_IS_RELEVANCY);
            }
            expenses1.setDeleted(1);
            wrapper.lambda().eq(Expenses::getId, id);
            expensesMapper.update(expenses1,wrapper);
        }else {
            expenses1.setDeleted(0);
            wrapper.lambda().eq(Expenses::getId, id);
            expensesMapper.update(expenses1,wrapper);
        }
    }
}
